﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Yunwei.Domain.Models;

namespace Yunwei.Web.Areas.Admin.Models
{
    public class VendorIngredientViewModel
    {
        public int Id { get; set; }

        [Required(ErrorMessage = "必填")]
        [StringLength(20, MinimumLength = 1, ErrorMessage = "{2}到{1}个字符")]
        [Display(Name = "名称")]
        public string Name { get; set; }

        [Required(ErrorMessage = "必填")]
        [Display(Name = "进价")]
        public decimal Cost { get; set; }

        [Required(ErrorMessage = "必填")]
        [Display(Name = "卖价")]
        public decimal Price { get; set; }

        [Required(ErrorMessage = "必填")]
        [Display(Name = "存货")]
        public decimal Stock { get; set; }

        [Required(ErrorMessage = "必填")]
        [StringLength(10, MinimumLength = 1, ErrorMessage = "{2}到{1}个字符")]
        [Display(Name = "单位")]
        public string Unit { get; set; }

        [StringLength(200, MinimumLength = 5, ErrorMessage = "{2}到{1}个字符")]
        [Display(Name = "照片")]
        public string Avatar { get; set; }

        [Display(Name = "详述")]
        public string Description { get; set; }

        public int Vendor_Id { get; set; }

        private List<string> units;
        public List<string> Units
        {
            get
            {
                if (units == null)
                {
                    units = new List<string>();
                    units.Add("份");
                    units.Add("克");
                    units.Add("两");
                    units.Add("斤");
                    units.Add("公斤");
                    units.Add("吨");
                    units.Add("块");
                    units.Add("罐");
                    units.Add("瓶");
                    units.Add("袋");
                    units.Add("把");
                    units.Add("个");
                    units.Add("台");
                    units.Add("箱");
                }

                return units;
            }
        }

        private List<SelectListItem> items;
        public List<SelectListItem> UnitItems
        {
            get
            {
                if (items == null)
                {
                    items = new List<SelectListItem>();
                    foreach (string unit in Units)
                    {
                        SelectListItem item = new SelectListItem()
                        {
                            Text = unit,
                            Value = unit
                        };

                        items.Add(item);
                    }
                }
                return items;
            }
        }


        public VendorIngredient VendorIngredient
        { 
            get
            {
                var vi = new VendorIngredient()
                {
                    Id = this.Id,
                    Cost = this.Cost,
                    Price = this.Price,
                    Stock = this.Stock,
                    Name = this.Name,
                    Unit = this.Unit,
                    Vendor_Id = this.Vendor_Id,
                    Avatar = this.Avatar,
                    Description = this.Description
                };

                return vi;
            }
        }

        public static VendorIngredientViewModel FromVendorIngredient(VendorIngredient vi)
        {
            VendorIngredientViewModel vivm = new VendorIngredientViewModel();
            if (vi == null) return vivm;

            vivm.Id = vi.Id;
            vivm.Cost = vi.Cost;
            vivm.Price = vi.Price;
            vivm.Stock = vi.Stock;
            vivm.Name = vi.Name;
            vivm.Unit = vi.Unit;
            vivm.Vendor_Id = vi.Vendor_Id;
            vivm.Avatar = vi.Avatar;
            vivm.Description = vi.Description;

            return vivm;
        }
    }
}